<template>
    <el-dialog
            title="大屏配置"
            :visible.sync="dialogVisible"
            width="500px">
        <el-form ref="form" :model="configData" label-width="80px">
            <el-form-item label="刷新时间" prop="cycle">
                <el-input size="small" @change="cycleChange" v-model="configData.cycle" placeholder="0表示不刷新"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doConfirm">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import {getStorage, setStorage} from '@/utils/storage';

export default {
  name: 'config',
  data() {
    return {
      dialogVisible: false,
      configData: {
        cycle: 5
      },
      timer: null
    }
  },
  mounted() {
    this.init()
  },
  destroyed() {
    clearInterval(this.timer)
  },
  methods: {
    cycleChange() {
      this.configData.cycle = parseInt(this.configData.cycle, 10) || 0
    },
    init() {
      this.getLocalConfig()
      const time = this.configData.cycle * 1000 || 0
      if (time) {
        this.timer = setInterval(() => {
          this.loadPage()
        }, time)
      } else {
        clearInterval(this.timer)
      }
    },
    open() {
      this.dialogVisible = true
      this.getLocalConfig()
    },
    close() {
      this.dialogVisible = false
    },
    loadPage() {
      this.$emit('load-page')
    },
    doConfirm() {
      clearInterval(this.timer)
      setStorage('local-config', this.configData)
      this.init()
      this.close()
    },
    getLocalConfig() {
      const configData = getStorage('local-config')
      if (configData) {
        this.configData = configData
      } else {
        this.configData.cycle = 5
      }
    }
  }
}
</script>

<style scoped>

</style>